package com.ztgf.commodity.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.CommoditySpecificationCategoryRelationBO;
import com.ztgf.commodity.models.bo.CommoditySpecificationCategoryRelationSaveBO;
import com.ztgf.commodity.models.dto.CommoditySpecificationCategoryRelationDTO;
import com.ztgf.commodity.models.service.ICommoditySpecificationCategoryRelationService;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 分类规格关系表控制器
 *
 * @author chenyu.wang
 * @Date 2020-05-27 11:05:46
 */

@RestController
@RequestMapping("/cms/commoditySpecificationCategoryRelation")
@Slf4j
@Api(value = "CommoditySpecificationCategoryRelationController", tags = "分类规格关系表接口")
public class CommoditySpecificationCategoryRelationController {

    @DubboReference
    private ICommoditySpecificationCategoryRelationService commoditySpecificationCategoryRelationService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加分类规格关系表", notes = "添加分类规格关系表")
    public Result insert(CommoditySpecificationCategoryRelationSaveBO saveBO) {
        boolean flag = commoditySpecificationCategoryRelationService.add(saveBO);
        if (flag) {
            return new Result<>();
        }
        return new Result<>(CodeEnum.FAIL_100007);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存分类规格关系表", notes = "保存分类规格关系表")
    public Result save(CommoditySpecificationCategoryRelationSaveBO saveBO) {
        if (saveBO.getId() == null) {
            //添加
            return insert(saveBO);
        } else {
            return update(saveBO);
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改分类规格关系表", notes = "修改分类规格关系表")
    public Result update(CommoditySpecificationCategoryRelationSaveBO saveBO) {
        CommoditySpecificationCategoryRelationDTO dto = new CommoditySpecificationCategoryRelationDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = commoditySpecificationCategoryRelationService.updateById(dto);
        if (flag) {
            return new Result<>();
        }
        return new Result<>(CodeEnum.FAIL_100008);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除分类规格关系表", notes = "删除分类规格关系表")
    @ApiImplicitParam(name = "id", value = "分类规格关系表id", required = true, paramType = "query", dataType = "Long")
    public Result delete(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = commoditySpecificationCategoryRelationService.deleteById(id);
        if (flag) {
            return new Result<>();
        }
        return new Result<>(CodeEnum.FAIL_100009);
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除分类规格关系表", notes = "批量删除分类规格关系表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        commoditySpecificationCategoryRelationService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<CommoditySpecificationCategoryRelationDTO> get(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        CommoditySpecificationCategoryRelationDTO dto = commoditySpecificationCategoryRelationService.selectById(id);
        return new Result<>(dto);
    }

    @PostMapping("/queryPageList")
    @ApiOperation(value = "分类规格关系表列表", notes = "分类规格关系表列表")
    public Result<Page<CommoditySpecificationCategoryRelationDTO>> queryPageList(CommoditySpecificationCategoryRelationBO bo) {
        Wrapper<CommoditySpecificationCategoryRelationDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());
        }
        Page<CommoditySpecificationCategoryRelationDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = commoditySpecificationCategoryRelationService.selectPage(page, wrapper);
        return new Result<>(page);
    }
}
